import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';


@Injectable()
export class Events{
	private subject:any;
	private asObservable:any;

	constructor(
	){
		this.subject = new Subject();
		this.asObservable = this.subject.asObservable();
	}


	//发送通知
	public publish(param:any):void{
		this.subject.next(param);
	}


	//接收通知
	public subscribe(val:string,method:any):any{
		this.asObservable.subscribe(msg => {
			if(val === msg && typeof msg === 'string'){
				method();
			}

			if(val === msg.name && typeof msg === 'object'){
				method(msg.param);
			}
		});
	}


	//销毁通知
	public unsubscribe(param:string):void{
		this.asObservable.unsubscribe(param);
	}


}